/**
 * This file is part of the ePNK project (Petri Net Kernel for eclipse). The ePNK
 * is platform for Petri net tools based on the PNML transfer format. Its
 * main idea is to provide generic Petri net types which can be easily plugged
 * in and a simple generic GMF editor, that can be used for graphically editing
 * nets of any plugged in type. Additional functionality can be plugged in.
 * 
 * Note that this project is still in an experimental phase, and is also used
 * as a case study for EMF/GMF based Model-based Software Engineering.
 * 
 * Copyright (C) 2010 Ekkart Kindler, eki@imm.dtu.dk
 * The license under which this software will be distributed is still to be decided -
 * it will probably be the EPL.
 *
 * $Id$
 */
package ar.edu.utn.frsf.cidisi.ginet.generation.upcolbpip2cginet.popup.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import ar.edu.utn.frsf.cidisi.ginet.generation.upcolbpip2cginet.Upcolbpip2GINet;
import ar.edu.utn.frsf.cidisi.upcolbpip.collaborativebusinessprocesses.CollaborativeBusinessProcess;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocol;


public class GenerateCGIMAFromChoreographyAction implements IObjectActionDelegate {
	
	private InteractionProtocol upcolbpipGIModule;
	
	private Shell shell;

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	final public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}	
	
	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	final public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			if (structuredSelection.size() == 1
					&& structuredSelection.getFirstElement() instanceof InteractionProtocol) {
				upcolbpipGIModule = (InteractionProtocol)structuredSelection.getFirstElement();
			}
		}
	}	
	
	/**
	 * @see IActionDelegate#run(IAction)
	 */
	final public void run(IAction action) {
		CollaborativeBusinessProcess collaborativeBP = (CollaborativeBusinessProcess) upcolbpipGIModule.eContainer();
		
		Upcolbpip2GINet upc2cgim = new Upcolbpip2GINet(upcolbpipGIModule);		
	
		upc2cgim.createConcreteGIModule();
		
		// Get the resource path 
		String path = collaborativeBP.eResource().getURI().path();
		
		// Get the file name
		String fileName = collaborativeBP.eResource().getURI().lastSegment();
		
		// The full path to the new PNML file
		String newFileName = path.substring(0, path.indexOf(fileName)) + "cgim.pnml";

		// Saves the net to a pnml file
		upc2cgim.saveGINet(newFileName);
		
		MessageDialog.openInformation(
				shell,
				"Transformation",
				"Page created.");	
	}	

}
